var path = "";
function getPath(pathVal) {
	path = pathVal;
}

var requiredArray = [ "#loginCode", "#orgCode", "#postCode" ];
var notRequiredArray = [ "#passwd", "#newPasswd", "#repeatNewPasswd", "#fax", "#unitWebsite" ];
var idArray = requiredArray.concat(notRequiredArray);

//必填项提示信息
var LOGIN_CODE_MSG = "请输入6~18个字符，可使用字母、数字、下划线，需以字母开头";
var PASSWD_MSG = "请输入6~16个字符，区分大小写";
var REPEAT_NEW_PASSWD_MSG = "请再次填写新密码";
var ORG_CODE_MSG = "请根据成立批文填写拟登记单位的第一名称";
var POST_CODE_MSG = "请输入单位邮政编码";
var CAPTCHA_MSG = "请输入验证码";

//不是必填项的提示信息
var OWNERID_MSG = "请选择单位所属的登记机关";
var FAX_MSG = "请输入单位传真号";
var UNIT_WEBSITE_MSG = "请填写单位的网站网址";

//格式不正确或错误时的提示信息
var EXIST_LOGIN_CODE_MSG = "用户名已存在";
var PASSWD_LIKE_LOGIN_CODE = "密码不能与用户名一样";
var NOT_RIGHT_PASSWD = "原密码不正确";
var NOT_EXIST_PASSWD = "请先输入原密码";
var NOT_EXIST_NEW_PASSWD = "请先输入新密码";
var REPEAT_PASSWD_ERROR_MSG = "两次输入的新密码不一致";
var ORG_CODE_ERROR_MSG = "单位名称不能超过250个字";
var EXIST_ORG_CODE_MSG = "单位名称已存在";
var POST_CODE_ERROR_MSG = "邮政编码格式不正确";
var UNIT_WEBSITE_ERROR_MSG = "单位的网站网址不能为数字";
var ERROR_MSG = "网络有误 ！请稍后重试";

var requiredInfoArray = [ LOGIN_CODE_MSG, ORG_CODE_MSG, POST_CODE_MSG ];

function noShowMsg(childId) {
	$(childId).removeClass("blueInput").removeClass("redInput");
	return $(childId).parent().next().html("");
}

function showCueMsg(childId, msg) {
	$(childId).addClass("blueInput");
	return $(childId).parent().next().html("<div class='cueImage'></div><div class='cueMsg'>" + msg + "</div>");
}

function showErrorMsg(childId, msg) {
	$(childId).removeClass("blueInput").addClass("redInput");
	return $(childId).parent().next().html("<div class='errorImage'></div><div class='errorMsg'>" + msg + "</div>");
}

function showSuccessImg(childId) {
	$(childId).removeClass("blueInput").removeClass("redInput");
	return $(childId).parent().next().html("<div class='successImg'></div><div class='cueMsg'></div>");
}

/**
 * 验证是否为空
 * @param id
 * @returns true 不为空，false 为空
 */
function validIsNotNull(id) {
	var isFlag = true;
	if ($(id).val() == null || $(id).val() == undefined || $.trim($(id).val()) == "") {
		isFlag = false;
	}
	return isFlag;
}

function validIsNaN(id) {
	var isFlag = true;
	if (isNaN($.trim($(id).val()))) {
		isFlag = false;
	}
	return isFlag;
}

function validNumLength(id, length) {
	var isFlag = true;
	if ($.trim($(id).val()).length < length) {
		isFlag = false;
	}
	return isFlag;
}

function ajaxValidLoginCode(url, id) {
	var isFlag = false;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {loginCode : $(id).val()},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = true;
		}
	});
	return isFlag;
}

function ajaxValidIsRightPasswd(url, id) {
	var isFlag = false;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {
			usrId : $("#sysUsrInfId").val(),
			passwd : $(id).val()
		},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = false;
		}
	});
	return isFlag;
}

function ajaxValidOrgCode(url, id) {
	var isFlag = false;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {orgCode : $(id).val()},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = true;
		}
	});
	return isFlag;
}

//验证用户名
function validLoginCode(id,oldId) {
	var isFlag = true;
	var re = /^[a-zA-Z].{5,17}$/;
	if ($(id).val() == $(oldId).val()) {
		showSuccessImg(id);
		isFlag = true;
	} else {
		if (!validIsNotNull(id) || !validNumLength(id, 6) || !re.test($(id).val())) {
			showErrorMsg(id, LOGIN_CODE_MSG);
			isFlag = false;
		} else if (ajaxValidLoginCode(path + "/isExistLoginCode.do", id)) {
			showErrorMsg(id,EXIST_LOGIN_CODE_MSG);
			isFlag = false;
		} else {
			showSuccessImg(id);
			isFlag = true;
		}
	}
	return isFlag;
}

//验证密码
function validPassword(id,loginCodeId) {
	var isFlag = true;
	if(validIsNotNull(id)) {
		if (!validNumLength(id, 6)) {
			showErrorMsg(id, PASSWD_MSG);
			isFlag = false;
		} else if ($.trim($(id).val()) == $.trim($(loginCodeId).val())) {
			showErrorMsg(id, PASSWD_LIKE_LOGIN_CODE);
			isFlag = false;
		} else if(!ajaxValidIsRightPasswd(path + "/isRightPasswd.do",id)) { 
			showErrorMsg(id, NOT_RIGHT_PASSWD);
			isFlag = false;
		} else {
			showSuccessImg(id);
			isFlag = true;
		}
	}
	return isFlag;
}

//验证新密码
function validNewPassword(oldId, newId, loginCodeId) {
	var isFlag = true;
	if (validIsNotNull(oldId) && validIsNotNull(newId)) {
		if (!validNumLength(newId, 6)) {
			showErrorMsg(newId, PASSWD_MSG);
			isFlag = false;
		} else if ($.trim($(newId).val()) == $.trim($(loginCodeId).val())) {
			showErrorMsg(newId, PASSWD_LIKE_LOGIN_CODE);
			isFlag = false;
		} else {
			showSuccessImg(newId);
			isFlag = true;
		}
	} else if (!validIsNotNull(oldId) && validIsNotNull(newId)) {
		showErrorMsg(newId, NOT_EXIST_PASSWD);
		isFlag = false;
	} else {
		noShowMsg(newId);
		isFlag = true;
	}
	return isFlag;
}

//验证确认新密码
function validRepeatNewPassword(oldId, newId, repeatId) {
	var isFlag = true;
	if (validIsNotNull(oldId) && validIsNotNull(newId) && validIsNotNull(repeatId)) {
		if (!validNumLength(repeatId, 6)) {
			showErrorMsg(repeatId, PASSWD_MSG);
			isFlag = false;
		} else {
			if ($.trim($(repeatId).val()) != $.trim($(newId).val())) {
				showErrorMsg(repeatId, REPEAT_PASSWD_ERROR_MSG);
				isFlag = false;
			} else {
				showSuccessImg(repeatId);
				isFlag = true;
			}
		}
	} else if(!validIsNotNull(oldId) && !validIsNotNull(newId) && validIsNotNull(repeatId)) {
		showErrorMsg(repeatId, NOT_EXIST_PASSWD);
		isFlag = false;
	} else if(validIsNotNull(oldId) && !validIsNotNull(newId) && validIsNotNull(repeatId)) {
		showErrorMsg(repeatId, NOT_EXIST_NEW_PASSWD);
		isFlag = false;
	} else if(validIsNotNull(oldId) && validIsNotNull(newId) && !validIsNotNull(repeatId)) {
		showErrorMsg(repeatId, REPEAT_NEW_PASSWD_MSG);
		isFlag = false;
	} else {
		noShowMsg(repeatId);
		isFlag = true;
	}
	return isFlag;
}

//验证登记机关
function validDjjg(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, OWNERID_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证单位名称
function validOrgCode(id,oldId) {
	var isFlag = true;
	if(!validIsNotNull("#sydwCode")){
		if ($(id).val() == $(oldId).val()) {
			if (validIsNotNull("#sydwCode")) {
				noShowMsg(id);
			} else {
				showSuccessImg(id);
			}
			isFlag = true;
		} else {
			if (!validIsNotNull(id)) {
				showErrorMsg(id, ORG_CODE_MSG);
				isFlag = false;
			} else if ($(id).val().length > 250) {
				showErrorMsg(id, ORG_CODE_ERROR_MSG);
				isFlag = false;
			} else if(ajaxValidOrgCode(path + "/isExistOrgCode.do", id)) {
				showErrorMsg(id, EXIST_ORG_CODE_MSG);
				isFlag = false;
			} else {
				if (validIsNotNull("#sydwCode")) {
					noShowMsg(id);
				} else {
					showSuccessImg(id);
				}
				isFlag = true;
			}
		}
	}
	return isFlag;
}

//验证传真
function validFax(id) {
	if (!validIsNotNull(id)) {
		noShowMsg(id);
	} else {
		showSuccessImg(id);
	}
	return true;
}

//验证邮政编码
function validPostCode(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, POST_CODE_MSG);
		isFlag = false;
	} else if (!validIsNaN(id) || !validNumLength(id, 6)) {
		showErrorMsg(id, POST_CODE_ERROR_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

// 验证单位网站
function validURL(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		noShowMsg(id);
	} else {
		if (validIsNaN(id)) {
			showErrorMsg(id, UNIT_WEBSITE_ERROR_MSG);
			isFlag = false;
		} else {
			showSuccessImg(id);
			isFlag = true;
		}
	}
	return isFlag;
}

function focusInfo() {
	$(requiredArray).each(function(i) {
		$(requiredArray[i]).focus(function() {
			$(this).attr('title', requiredInfoArray[i]);
			showCueMsg(this, requiredInfoArray[i]);
		});
	});
}

function blurInfo() {
	$(requiredArray[0]).blur(function() {
		validLoginCode($(this),"#oldLoginCode");
	});
	$(requiredArray[1]).blur(function() {
		validOrgCode($(this),"#oldOrgCode");
	});
	$(requiredArray[2]).blur(function() {
		validPostCode($(this));
	});
	$(notRequiredArray[0]).blur(function() {
		validPassword($(this),requiredArray[0]);
	});
	$(notRequiredArray[1]).blur(function() {
		validNewPassword(notRequiredArray[0], $(this), requiredArray[0]);
	});
	$(notRequiredArray[2]).blur(function() {
		validRepeatNewPassword(notRequiredArray[0], notRequiredArray[1], $(this));
	});
	$(notRequiredArray[3]).blur(function() {
		validFax($(this));
	});
	$(notRequiredArray[4]).blur(function() {
		validURL($(this));
	});
}

function getLength(id){
	var str = $(id).val();
	return str.replace(/[^\x00-\xff]/g,"**").length;
}

$(function() {
	if(validIsNotNull(requiredArray[1]) && getLength(requiredArray[1]) <= 50){
		$(".page div[class='ht60'] div[class='lineLeft2']").removeClass("lineLeft2").addClass("lineLeft");
		$(".page div[class='ht60'] div[class='lineLeft2_1']").removeClass("lineLeft2_1").addClass("lineLeft3");
		$(".page div[class='ht60'] div[class='lineRight2'] textarea").css("height","22px");
		$(".page div[class='ht60'] div[class='lineRight2']").removeClass("lineRight2").addClass("lineRight");
		$(".page div[class='ht60']").removeClass("ht60").addClass("ht35");
	}
	focusInfo();
	blurInfo();
});

function validAll(path) {
	var isFlag = true;
	if (!validLoginCode(requiredArray[0], "#oldLoginCode")) {
		isFlag = false;
	}
	if (!validPassword(notRequiredArray[0], requiredArray[0])) {
		isFlag = false;
	}
	if (!validNewPassword(notRequiredArray[0], notRequiredArray[1], requiredArray[0])) {
		isFlag = false;
	}
	if (!validRepeatNewPassword(notRequiredArray[0], notRequiredArray[1], notRequiredArray[2])) {
		isFlag = false;
	}
	if (!validOrgCode(requiredArray[1], "#oldOrgCode")) {
		isFlag = false;
	}
	validFax(notRequiredArray[3]);
	if (!validPostCode(requiredArray[2])) {
		isFlag = false;
	}
	if (!validURL(notRequiredArray[4])) {
		isFlag = false;
	}
	return isFlag;
}

document.onkeydown = function(e) {
	var theEvent = window.event || e;
	var code = theEvent.keyCode || theEvent.which;
	if (code == 13) {
		$("#toSave").click();
	}
};